package org.cklxh.waptime;

import java.io.IOException;

import org.cklxh.apn.ApnPojo;

import android.content.Context;
import android.os.Message;

/**
 * 此线程专门执行访问网络等操作，避免出现卡盾的情况。
 * @author Administrator
 *
 */
public class RefreshTimeThread extends Thread {

	@Override
	public void run() {
		WaptimeActivity waptimeActivity=WaptimeActivity.getActivityObject();
		Context context=waptimeActivity.getBaseContext();
		WaptimeHandler waptimeHandler=waptimeActivity.getWaptimeHandler();
		
		ApnPojo apnPojo=null;
		try {
			NetWorkServer netWorkServer=new NetWorkServer();
			apnPojo=netWorkServer.getActiveNetworkApn(context);
		} catch (Exception e) {
			//Log.v("error", "得到活动的连接出错！", e);
			//提示错误信息。
			Message msg = waptimeHandler.obtainMessage(1, 1, 1, e.getMessage());
			waptimeHandler.sendMessage(msg);
			//恢复网络时间的初始显示。
			Message msg1 = waptimeHandler.obtainMessage(3);
			waptimeHandler.sendMessage(msg1);
			
			return;
		}
		/*if(apnPojo==null){
			waptimeActivity.showMsg("正在通过WIFI连接网络。");
		}else{
			waptimeActivity.showMsg("正在通过"+apnPojo.getName()+"("+apnPojo.getApn()+")连接网络。");
		}
		*/
		
		TimeServer timeServer=new TimeServer();
		long time;
		try {
			time = timeServer.getWebTime("http://wap.baidu.com/",apnPojo);
		} catch (IOException e) {
			Message msg = waptimeHandler.obtainMessage(1, 1, 1, "得到时间出错，请重试！");
			waptimeHandler.sendMessage(msg);
			Message msg1 = waptimeHandler.obtainMessage(3);
			waptimeHandler.sendMessage(msg1);
			return;
		}
		
		//在此线程中，无法直接操作界面，需要发送回主线程。
		Message msg = waptimeHandler.obtainMessage(2, 1, 1, time);
		waptimeHandler.sendMessage(msg);
		
	}

	
}
